package wangyihu;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String s = buf.readLine();
        String[] ss;
        int T = Integer.parseInt(s);
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < T; t++){
            s = buf.readLine();
            int N = Integer.parseInt(s);
            s = buf.readLine();
            ss = s.split(" ");
            int gamer_atk = Integer.parseInt(ss[0]);
            int gamer_def = Integer.parseInt(ss[1]);
            int gamer_hp = Integer.MAX_VALUE/2;
            int min_gamer_hp = Integer.MAX_VALUE/2;
            int n = 0;
            int passive = 0;
            boolean flag = false;
            for (; n < N; n++){
                min_gamer_hp = Math.min(min_gamer_hp, gamer_hp);
                gamer_hp += passive;
                s = buf.readLine();
                ss = s.split(" ");
                int monster_atk = Integer.parseInt(ss[0]);
                int monster_def = Integer.parseInt(ss[1]);
                int monster_hp = Integer.parseInt(ss[2]);
                //主角攻击力小于怪物防御力
                if (gamer_atk <= monster_def && !flag){
                    sb.append(-1).append("\n");
                    flag = true;
                }
                if(!flag){
                    int gamer_hurt = gamer_atk - monster_def;
                    int monster_hurt = monster_atk < gamer_def?0:(monster_atk - gamer_def);
                    int count;
                    if (monster_hp%gamer_hurt == 0){
                        count = monster_hp/gamer_hurt - 1;
                        gamer_hp -= count * monster_hurt;
                        passive = 0;
                    } else {
                        count = monster_hp/gamer_hurt;
                        int tmp = monster_hp%gamer_hurt;
                        gamer_hp -= count*monster_hurt;
                        passive = (gamer_hurt - tmp);

                    }
                }

            }
            if (!flag){
                min_gamer_hp = Math.min(min_gamer_hp, gamer_hp);
                sb.append(Integer.MAX_VALUE/2 - min_gamer_hp + 1).append("\n");
            }

        }
        System.out.print(sb.toString());
    }
}
